#include "com/find_com.hpp"

void Find_Com::getComInfoList() {
  this->comInfoList = QSerialPortInfo::availablePorts();
  QStringList comInfoList;
  if (this->emptyComInfoList()) {
    qDebug() << "comInfoList is empty";
    return;
  }
  // 将comInfoList转换为QStringList
  for (int i = 0; i < this->comInfoList.length(); i++) {
    comInfoList.append(this->comInfoList[i].portName());
  }
  // 如果comInfoList为空，返回
  if (comInfoList.empty()) {
    qDebug() << "comInfoList is empty";
    return;
  }
  // 测试信号是否发送
  // qDebug() << "emit comInfoListChanged signal";
  emit this->comInfoListChanged(comInfoList);
  // for (auto info : comInfoList) {
  //   qDebug() << info;
  // }
  return;
}

bool Find_Com::emptyComInfoList() {
  if (this->comInfoList.isEmpty()) {
    return true;
  } else {
    return false;
  }
}

void Find_Com::printComInfoList() {
  if (this->emptyComInfoList()) {
    qDebug() << "comInfoList is empty";
    return;
  } else {
    for (int i = 0; i < this->comInfoList.length(); i++) {
      qDebug() << this->comInfoList[i].portName();
    }
  }
}
